﻿Ext.onReady(function() {

	var dataRecord = Ext.data.Record.create([]);

 
   	var store = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
						url : ''}),
				reader : new Ext.data.JsonReader({totalProperty:'results',id :'columncode'},dataRecord)
			});

		
	var grid = new Ext.grid.GridPanel({
			store : store,
            renderTo : 'test',
			tbar :[
{text : '执行sql',iconCls : 'page_goto',
	 handler : function() {

				var str="sql="+tbar2.get(1).getValue();

	Ext.lib.Ajax.request('POST', 'hello.jsp', {
		success : function(request) {
			var message = request.responseText;
			var jsonObj = Ext.util.JSON.decode(message);
			var colm=new Array(jsonObj.length);
			for(var i=0;i<jsonObj.length;i++){
		      colm[i]={id :jsonObj[i].name,header :jsonObj[i].name,tooltip :'列名:'+jsonObj[i].name,dataIndex : jsonObj[i].name,width :100,sortable : false,checksortable:true}
			}
			var colModel = new Ext.grid.ColumnModel(
				{
				  listeners:{columnmoved:function(){  }},
				  columns:colm
			     }
			 );

			var dataRecord = Ext.data.Record.create(jsonObj);
  	        var store = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({url : ''}),
				reader : new Ext.data.JsonReader({totalProperty:'results',id :'columncode'},dataRecord)});
						grid.reconfigure( store,colModel ) ;
			
			
		},
		failure : function(request) {
			Ext.MessageBox.show({
				title : '错误',
				msg :  request.responseText,
				buttons : Ext.Msg.OK,
				icon : Ext.MessageBox.ERROR

			});
			
		}
	},str);

}},       
			{text : '保存',iconCls : 'table_save',
			 handler : function() {var a=grid.colModel.config[1];
								for(var name in a )
								alert(name+"="+a[name]);				
				
				}
			}],
		columns:[],
	    width: 800,
	    height:120
	});


var pand=true;

              var  sortableItem=new Ext.menu.CheckItem({
                    text: "是否可排序",
                    checked: false,
                    checkHandler :function(item,checked){
                    	  grid.colModel.config[grid.view.hdCtxIndex].checksortable=checked;
                    },
                    hideOnClick:false
                });
                
                var  dataIndexItem=new Ext.menu.Item({ text: "",hideOnClick:false });
                
                 grid.view.hmenu.on('beforeshow',function(){
                 	var config=grid.colModel.config[grid.view.hdCtxIndex];
                 	var checked=config.checksortable;
                 	dataIndexItem.setText("列名:"+config.dataIndex);
                    sortableItem.setChecked(checked); 
				});
grid.view.hmenu.add('-',sortableItem ,dataIndexItem);


var tbar2 = new Ext.Toolbar({renderTo : grid.tbar,items:['sql:',{xtype : 'textarea',width   :762,height :180}]});

grid.on('headerdblclick',function (grid,Number, e){
if(pand){
	pand=false;
var tf=new Ext.form.TextField({grow:true,width :15,height:16,listeners :{
		blur:function(){ 
			if(this.getValue()!='')
			 grid.colModel.setColumnHeader(this.colnumber,this.getValue());
			else
			 grid.colModel.setColumnHeader(this.colnumber,grid.colModel.getColumnHeader(this.colnumber));
			pand=true;}
	 	}
	});
  e.target.innerText='';
  tf.colnumber=Number;
  tf.render(e.target);
  tf.focus();
  grid.tf=tf;
}else{
		grid.tf.blur();
		pand=true;

}


	

});
});
